﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DOTNETGROUP.Data;
using DOTNETGROUP.Entities;
using System.Data;

public partial class Admin_MauWeb_CNMauWeb : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        imgViewer.ImageUrl = H_AnhDaiDien.Value.Length > 0 ? Common.sPathUpMauWebNho + H_AnhDaiDien.Value : "~/Images/no_image.jpg";
        if (!Page.IsPostBack)
        {
            BindCatalog.BindLoaiMauWeb(cboLoaiMauWeb);
            if (Request.QueryString["id"] != null)
            {
                H_CatalogId.Value = Request.QueryString["id"].ToString();
                BindData(Convert.ToInt32(H_CatalogId.Value));
            }
        }
    }
    protected void BindData(int IdCategories)
    {
        using (MauWeb ent_MauWeb = DataRepository.MauWebProvider.GetById(IdCategories))
        {
            txtTieuDe.Text = ent_MauWeb.TieuDe;
            cboLoaiMauWeb.SelectedValue = ent_MauWeb.IdLoaiMauWeb.ToString();
            H_AnhDaiDien.Value = ent_MauWeb.AnhDaiDien;
            imgViewer.ImageUrl = ent_MauWeb.AnhDaiDien.Length > 0 ? Common.sPathUpMauWebNho + ent_MauWeb.AnhDaiDien : "no_image.jpg";
            txtTomtat.Text = ent_MauWeb.TomTat;
            txtDiemSo.Text = ent_MauWeb.DiemSo.ToString();
        }
    }

    protected void cmdLuu_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            using (MauWeb ent_MauWeb = new MauWeb() { IdLoaiMauWeb = Convert.ToInt32(cboLoaiMauWeb.SelectedValue), TieuDe = Common.StandardString(txtTieuDe.Text), TomTat = Common.StandardString(txtTomtat.Text), DiemSo = Convert.ToInt32(txtDiemSo.Text)})
            {
                ent_MauWeb.Id = Convert.ToInt32(H_CatalogId.Value);
                if (H_AnhDaiDien.Value.Length == 0)
                {
                    ent_MauWeb.AnhDaiDien = "no_image.jpg";
                }
                else
                {
                    ent_MauWeb.AnhDaiDien = Common.StandardString(H_AnhDaiDien.Value);
                }
                ent_MauWeb.TrangThai = chkTrangThai.Checked;
                ent_MauWeb.NgayCapNhat = Common.GetServerDateTime();
                if (ent_MauWeb.Id == 0)
                {
                    updateStatus = DataRepository.MauWebProvider.Insert(ent_MauWeb);
                }
                else
                {
                    //ent_MauWeb.Id = Convert.ToInt32(H_CatalogId.Value);
                    ent_MauWeb.EntityState = EntityState.Changed;
                    updateStatus = DataRepository.MauWebProvider.Update(ent_MauWeb);
                }
                if (!updateStatus)
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.MAUWEB_CAPNHAT_THATBAI, true);
                else
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.MAUWEB_CAPNHAT_THANHCONG, true);
                    RedirectTo("~/Admin/MauWeb/QLMauWeb.aspx");
                }
            }

        }

        catch (Exception ex)
        {
            Common.WriteLog("CNMauWeb.aspx.cs", "cmdLuu_Click", ex.ToString());
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.MAUWEB_CAPNHAT_THATBAI, true);
        }
    }

    private void RedirectTo(string url)
    {
        string redirectURL = Page.ResolveClientUrl(url);
        string script = String.Format("window.location = '{0}';", redirectURL);
        ScriptManager.RegisterStartupScript(this, typeof(Page), "RedirectTo", script, true);
    }

    protected void cmdNhapTiep_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "";
        H_AnhDaiDien.Value = "no_image.jpg";
        txtTomtat.Text = "";
        chkTrangThai.Checked = true;
        txtTieuDe.Text = "";
        txtDiemSo.Text = "";
        BindCatalog.BindLoaiMauWeb(cboLoaiMauWeb);
    }
}